(function ($) {
    $(function () {
        var print = {
            globalOptions: {
                orderData: {
                    orderId: 0
                }
            },
            print: function () {
                var that = this;
                $('.print_printbtn_item').click(function () {
                    $('.print_page_wrapper').printArea({
                        mode: 'iframe'
                    });
                    setTimeout(function () {
                        $('.print_preview_wrapper').modal('hide');
                        $('.print_confirm_wrapper').modal('show');
                    }, 2000);
                });
                $('.print_successbtn_item').click(function () {
                    $('.print_confirm_wrapper').modal('hide');
                    alert('aaa')
                    that.printsuccessRequest({
                        id: that.globalOptions.orderData.orderId,
                        status: 11
                    }, function (data) {

                        console.log(that.globalOptions.orderData.orderId)
                        console.log(data);
                        if(data.code==1){
                            location.reload();
                        }
                        // orderitemEl.find('.print_accreditationbtn_item').attr('disabled', true);
                        // orderitemEl.find('.print_reviewbtn_item').attr('disabled', false);
                    })
                });
                $('.print_faildbtn_item').click(function () {
                    $('.print_confirm_wrapper').modal('hide');
                    that.printagainRequest({
                        id: that.globalOptions.orderData.orderId
                    }, function (data) {
                        console.log(that.globalOptions.orderData.orderId)
                        console.log(data);
                        if(data.code===0){
                            location.reload();
                        }
                    })
                });
                $('.print_remakebtn_item').click(function(){
                    $('.print_confirm_wrapper').modal('hide');
                    that.pullbackRequest({
                        id: that.globalOptions.orderData.orderId,
                        status: 10
                    }, function (data) {
                        console.log(that.globalOptions.orderData.orderId)
                        console.log(data);
                        if(data.code===0){
                            location.reload();
                        }
                    })
                })

            },
            bindData: function (thisObj) {
                var that = this,
                    thisBtn = thisObj,
                    orderId = thisBtn.attr('itemid'),
                    name = thisBtn.attr('itemname'),
                    passportNO = thisBtn.attr('itempassportno'),
                    sex = thisBtn.attr('sex'),
                    birthday = thisBtn.attr('itembirthday'),
                    itemsingaddress = thisBtn.attr('itemsingaddress'),
                    itemsigndate = thisBtn.attr('itemsigndate'),
                    signUnti = thisBtn.attr('itemsignunti'),
                    status = thisBtn.attr('status');

                that.globalOptions.orderData.orderId = orderId;

                //绑定数据
                //conslo.log(id+"我的ID");
                $(".name").html(name);
                $(".passportNO").html(passportNO);
                $(".sex").html(sex);
                var ayear = birthday.substr(0, 4);
                var amonth = birthday.substr(4, 2);
                var aday = birthday.substr(6, 2);
                console.log(ayear + "_" + amonth + "_" + aday);
                var adate = ayear + "年" + amonth + "月" + aday + "日";
                $(".birthday").html(adate);

                $("._ID").val(orderId);
                $("._STATUS").val(status);

                //绑定数据
                //conslo.log(id+"我的ID");
                $(".name").html(name);
                $(".passportNO").html(passportNO);
                $(".sex").html(sex);
                var ayear = birthday.substr(0, 4);
                var amonth = birthday.substr(4, 2);
                var aday = birthday.substr(6, 2);
                console.log(ayear + "_" + amonth + "_" + aday);
                var adate = ayear + "年" + amonth + "月" + aday + "日";
                $(".birthday").html(adate);

                //获取当前系统的时间
                var date = new Date();
                var seperator1 = "-";
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var currentdate = year + seperator1 + month + seperator1 + strDate;

                var str = currentdate.split("-");
                var bdate = str[0] + "年" + str[1] + "月" + str[2] + "日";
                if (str[0].length > 4) {
                    var byear = signDate.substr(0, 4);
                    var bmonth = signDate.substr(4, 2);
                    var bday = signDate.substr(6, 2);
                    bdate = byear + "年" + bmonth + "月" + bday + "日";
                }
                $(".signDate").html(bdate);
                $(".signUnti").html(signUnti);

                // console.log(signDate+"---"+str[0]+"*"+str[1]+"*"+str[2]);
                console.log(currentdate + "我的时间" + str[0] + "*" + str[1] + "*" + str[2]);

                var bdate = str[0] + "年" + str[1] + "月" + str[2] + "日";
                if (str[0].length > 4) {
                    var byear = signDate.substr(0, 4);
                    var bmonth = signDate.substr(4, 2);
                    var bday = signDate.substr(6, 2);
                    bdate = byear + "年" + bmonth + "月" + bday + "日";
                }
                $(".signDate").html(bdate);
                $(".signUnti").html(signUnti);

                $('.qrcode').qrcode({
                    width: 54,
                    height: 54,
                    text: passportNO,
                    correctLevel: 1,
                    typeNumber: -1, //计算模式
                });
                var canvas = $('.qrcode canvas');
                var img = canvas[0].toDataURL("image/png");
                console.log("生成二维码");

                $('.qrcode').html("<img src='" + img + "'>");

                $('.print_statistics_wrapper').modal('show');
            },
            printsuccessRequest: function (options, callback) {
                $.ajax({
                    type: 'post',
                    url: 'http://localhost:8080/gov/passport/updateStatus',
                    datatype: 'json',
                    data: options,
                    success: function (data) {
                        callback(data);
                    }
                })
            },
            printagainRequest: function (options, callback) {
                $.ajax({
                    type: 'post',
                    url: 'http://localhost:8080/gov/passport/againPrint',
                    dataType: 'json',
                    data: options,
                    success: function (data) {
                        callback(data);
                    }
                })
            },
            pullbackRequest: function (options, callback) {
                $.ajax({
                    type: 'post',
                    url: 'http://localhost:8080/gov/passport/againPrint',
                    dataType: 'json',
                    data: options,
                    success: function (data) {
                        callback(data);
                    }
                })
            },
            printSettings: function () {
                $('.print_settingsbtn_item').click(function () {
                    $('.print_settings_wrapper .maincontent').toggle('fast')
                });

                $('.print_leftpadding_wrapper').priceCalculator({
                    onchange: (function (value) {
                        var initValue = $('.print_product_wrapper .my_show').css('padding-left'),
                            paddingLeft;
                        initValue = parseInt(initValue.replace('px', ''))
                        return function (value) {
                            paddingLeft = parseInt(value)
                            console.log(value)
                            console.log(initValue)
                            // $('.print_preview_wrapper .my_show').css({
                            // 	position: 'relative',
                            // 	right: paddingLeft
                            // })
                            $('.print_product_wrapper .my_show').css({
                                position: 'relative',
                                left: paddingLeft
                            })
                        }
                    })()
                });

                $('.print_toppadding_wrapper').priceCalculator({
                    onchange: (function (value) {
                        var initValue = parseInt($('.print_product_wrapper .my_show').css('margin-top').replace('px', '')),
                            paddingTop;
                        return function (value) {
                            paddingTop = initValue + parseInt(value);
                            console.log(value)
                            console.log(initValue)
                            // $('.print_preview_wrapper .my_show').css({
                            // 	position: 'relative',
                            // 	top: paddingTop
                            // })
                            $('.print_product_wrapper .my_show').css({
                                position: 'relative',
                                top: paddingTop,
                                // transform: 'rotate(180deg)'
                            })
                        }
                    })()
                });
            },
            test: function () {
                $('.aaa').click(function () {
                    var value = $('.aaa_input').val();
                    // $('.selector').val(value);
                    $('.selector').append('<option>' + value + '</option>')
                    alert($('.selector').val())
                })
            },
            main: function () {
                var that = this;
                $('.print_faildbtn_item').click(function () {
                    $('.print_confirm_wrapper').modal('hide');
                });

                $('.print_accreditationbtn_item').click(function () {
                    var $this = $(this);
                    that.bindData($this);
                    that.print();
                    console.log(that.globalOptions.orderData.orderId)
                });


                $('.print_next_item').click(function () {
                    $('.print_statistics_wrapper').modal('hide');
                    $('.print_preview_wrapper').modal('show');
                })
                $('.print_remakebtn_item').click(function () {
                    $('.print_remakedialog_wrapper').modal('show');
                })
                $('.print_cancelremakebtn_item').click(function () {
                    $('.print_remakedialog_wrapper').modal('hide');
                })
                that.test();
                that.printSettings();
            },
            init: function () {
                this.main();
            }
        }
        print.init();

        var useless = {
            checkOK: function (id, status) {
                var url = "${ctx}/passport/updateStatus";
                $.post(url, {
                    "id": id,
                    "status": status
                }, function (data) {
                    if (data.code == 0) {
                        $('#myModal_' + id).modal('hide');
                        //window.location.reload();
                    }
                }, "json");
            },
            cmdPrint: function () {
                var id = $("._ID").val();
                var status = $("._STATUS").val();
                console.log("状态码：" + status);
                var url = "http://jd.cvssp.cn/passport/updateStatus";
                $.post(url, {
                    "id": id,
                    "status": status
                }, function (data) {
                    if (data.code == 0) {
                        $('.print_statistics_wrapper').modal('hide');
                        $('#myModal3').modal('show');
                    } else {
                        $('.print_statistics_wrapper').modal('hide');
                        $('#myModal3').modal('hide');
                    }
                }, "json");
            },
        }

        function btns() {
            var n = 10;
            var btn = $("btn");
            btn.text("重制(" + n + ")");
            if (n > 0) {
                btn.setAttribute('disabled', 'true');
                n--;
                btn.text = "重制(" + n + ")";
                var time = setTimeout(btns, 1000);
            } else {
                setTimeout(time);
                btn.removeAttribute('disabled');
                btn.value = "重制";
                n = 10;
            }
        }

        function checkAgainPrint(id) {
            var url = "${ctx}/passport/checkAgainPrint";
            $.post(url, {
                "id": id
            }, function (data) {
                if (data.code == 0) {
                    $('.print_statistics_wrapper').modal('hide');
                    $('#myModal3').modal('hide');
                    window.location.reload();
                }
            }, "json");
        }

        function detail(div_id) {
            $('#' + div_id).modal('show');
        }

        function close3() {
            $('#myModal3').modal('hide');
            window.location.reload();
        }

        function search() {
            var cardNO = $('#cardNO').val();
            var num = $('#num').val();
            if (cardNO == "" && num == "") {
                alert("请至少输入一个查询项");
                return;
            }
            window.location.href = "${ctx}/passport/waiteSearch?cardNO=" + cardNO + "&num=" + num;
        }
    })
})(jQuery);
